package com.wanmait.movies.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Random;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;

import com.wanmait.movies.dao.ActorDao;
import com.wanmait.movies.dao.ClickDao;
import com.wanmait.movies.dao.CollectDao;
import com.wanmait.movies.dao.DirectorDao;
import com.wanmait.movies.dao.DiscussDao;
import com.wanmait.movies.dao.GiveGradesDao;
import com.wanmait.movies.dao.ManegerDao;
import com.wanmait.movies.dao.MovieActorDao;
import com.wanmait.movies.dao.MovieCountryDao;
import com.wanmait.movies.dao.MovieDao;
import com.wanmait.movies.dao.MovieDirectorDao;
import com.wanmait.movies.dao.MovieLanguageDao;
import com.wanmait.movies.dao.MovieTypeDao;
import com.wanmait.movies.dao.ReportDao;
import com.wanmait.movies.util.ImageSize;
import com.wanmait.movies.vo.Actor;
import com.wanmait.movies.vo.AddMovieRecords;
import com.wanmait.movies.vo.DeleteMovieRecords;
import com.wanmait.movies.vo.Director;
import com.wanmait.movies.vo.Maneger;
import com.wanmait.movies.vo.MonfinMovieRecords;
import com.wanmait.movies.vo.Movie;
import com.wanmait.movies.vo.MovieCountry;
import com.wanmait.movies.vo.MovieLanguage;
import com.wanmait.movies.vo.MovieType;
import com.wanmait.movies.vo.Report;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import sun.management.MonitorInfoCompositeData;

/**
 * Servlet implementation class ManegerServlet
 */
@WebServlet("/ManegerServlet")
@MultipartConfig
public class ManegerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ManegerServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String action = request.getParameter("action");
		
		if("requestAddMovie".equals(action)) {
			requestAddMovie( request, response);
		}
		if("findDirector".equals(action)) {
			findDirector( request, response);
		}
		if("addDirector".equals(action)) {
			addDirector( request, response);
		}
		if("addActor".equals(action)) {
			addActor( request, response);
		}
		if("findActor".equals(action)) {
			findActor( request, response);
		}
		if("addLanguage".equals(action)) {
			addLanguage( request, response);
		}
		if("addType".equals(action)) {
			addType( request, response);
		}
		if("addCountry".equals(action)) {
			addCountry( request, response);
		}
		if("languageList".equals(action)) {
			languageList( request, response);
		}
		if("countryList".equals(action)) {
			countryList( request, response);
		}
		if("typeList".equals(action)) {
			typeList( request, response);
		}
		if("submitMovie".equals(action)) {
			submitMovie( request, response);
		}
		if("creatCode".equals(action)) {
			creatCode( request, response);
		}
		if("checkLog".equals(action)) {
			checkLog( request, response);
		}
		if("getManeger".equals(action)) {
			getManeger( request, response);
		}
		if("requestDeleteMovie".equals(action)) {
			requestDeleteMovie( request, response);
		}
		if("findMovie".equals(action)) {
			findMovie( request, response);
		}
		if("deleteMovie".equals(action)) {
			deleteMovie( request, response);
		}
		if("requestManegerReport".equals(action)) {
			requestManegerReport( request, response);
		}
		if("agreeReport".equals(action)) {
			agreeReport( request, response);
		}
		if("disagreeReport".equals(action)) {
			disagreeReport( request, response);
		}
		if("requestModifymovie".equals(action)) {
			requestModifymovie( request, response);
		}
		if("findDeleteMovie".equals(action)) {
			findDeleteMovie( request, response);
		}
		if("getModifyMovie".equals(action)) {
			getModifyMovie( request, response);
		}
		if("modifyMovie".equals(action)) {
			modifyMovie( request, response);
		}
		
		if("requestManeger".equals(action)){
			
			requestManeger( request, response);
		}
		if("getReportCount".equals(action)) {
			getReportCount( request, response);
		}
		
		if("getReportHistory".equals(action)) {
			getReportHistory( request, response);
		}
		if("getMovieReportList".equals(action)) {
			getMovieReportList( request, response);
		}
		if("requestMessagejsp".equals(action)) {
			requestMessagejsp( request, response);
		}
		if("getActorList".equals(action)) {
			getActorList( request, response);
		}
		if("getDirectorList".equals(action)) {
			getDirectorList( request, response);
		}
		if("getLanguageList".equals(action)) {
			getLanguageList( request, response);
		}
		if("getCountryList".equals(action)) {
			getCountryList( request, response);
		}
		if("getTypeList".equals(action)) {
			getTypeList( request, response);
		}
		if("pagedeletemovie".equals(action)){
			pagedeletemovie(request, response);
		}
		
		if("getReportHistoryAjax".equals(action)) {
			getReportHistoryAjax(request, response);
		}
		if("getAddHistoryAjax".equals(action)) {
			getAddHistoryAjax(request, response);
		}
		if("getDeleteHistoryAjax".equals(action)) {
			getDeleteHistoryAjax(request, response);
		}
		if("getModfinHistoryAjax".equals(action)) {
			getModfinHistoryAjax(request, response);
		}	

		if("modifyActor".equals(action)) {
			modifyActor(request, response);
		}
		if("modifyDirector".equals(action)) {
			modifyDirector(request, response);
		}
		if("modifyType".equals(action)) {
			modifyType(request, response);
		}
		if("modifyCountry".equals(action)) {
			modifyCountry(request, response);
		}
		if("modifyLanguage".equals(action)) {
			modifyLanguage(request, response);
		}
		
		if("findActorById".equals(action)) {
			findActorById(request, response);
		}
		
		if("findDirectorById".equals(action)) {
			findDirectorById(request, response);
		}
		if("findLanguageById".equals(action)) {
			findLanguageById(request, response);
		}
		if("findTypeById".equals(action)) {
			findTypeById(request, response);
		}
		if("findCountryById".equals(action)) {
			findCountryById(request, response);
		}
	}
	
	//查找地区通过id
	
		protected void findCountryById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String aid = request.getParameter("aid");
			MovieCountry country = MovieCountryDao.getDao().findById(Integer.parseInt(aid));
			JSONObject js =JSONObject.fromObject(country);
			String countrys=js.toString();
			
			PrintWriter pw =response.getWriter();
			pw.print(countrys);
		}
	//查找类型通过id
	
	protected void findTypeById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String aid = request.getParameter("aid");
		MovieType type = MovieTypeDao.getDao().findById(Integer.parseInt(aid));
		JSONObject js =JSONObject.fromObject(type);
		String types=js.toString();
		
		PrintWriter pw =response.getWriter();
		pw.print(types);
	}

	//查找语言通过id
	
		protected void findLanguageById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String aid = request.getParameter("aid");
			MovieLanguage language = MovieLanguageDao.getDao().findById(Integer.parseInt(aid));
			JSONObject js =JSONObject.fromObject(language);
			String languages=js.toString();
			
			PrintWriter pw =response.getWriter();
			pw.print(languages);
		}
	
	//查找演员通过id
	 
		protected void findActorById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String aid = request.getParameter("aid");
			Actor actor = ActorDao.getDao().findById(Integer.parseInt(aid));
			
			JSONObject js =JSONObject.fromObject(actor);
			String actors=js.toString();
		
			PrintWriter pw =response.getWriter();
			pw.print(actors);
		}
	//查找导演用过id
		
		protected void findDirectorById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String aid = request.getParameter("aid");
			Director director = DirectorDao.getDao().findById(Integer.parseInt(aid));
			JSONObject js =JSONObject.fromObject(director);
			String directors=js.toString();
			
			PrintWriter pw =response.getWriter();
			pw.print(directors);
		}
		
	//修改语言
		
		protected void modifyLanguage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String name =request.getParameter("name");
			String id =request.getParameter("id");
			
			MovieLanguageDao.getDao().updateLanguage(name, Integer.parseInt(id));
			String h = request.getHeader("referer");
			response.sendRedirect(h);
		
	}
    //修改类型
					
		protected void modifyType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		   String name =request.getParameter("name");
		   String id =request.getParameter("id");
								
			MovieTypeDao.getDao().updateType(name, Integer.parseInt(id));
			String h = request.getHeader("referer");
			response.sendRedirect(h);
							
     }
		 //修改地区
		
		protected void modifyCountry(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		   String name =request.getParameter("name");
		   String id =request.getParameter("id");
								
			MovieCountryDao.getDao().updateCountry(name, Integer.parseInt(id));
			String h = request.getHeader("referer");
			response.sendRedirect(h);
							
     }
	//修改演员
	
		protected void modifyActor(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String name =request.getParameter("name");
			String id =request.getParameter("id");
			String introduce =request.getParameter("introduce");
			Part part =request.getPart("image");//判断有没有修改图片
			String h1 = part.getHeader("content-disposition");
			
			
			if(h1.contains("filename=\"\"")) {
				ActorDao.getDao().updateActor(name, introduce,Integer.parseInt(id));
				String h = request.getHeader("referer");
				response.sendRedirect(h);
			}else {
			
				String filepath = request.getServletContext().getRealPath("/images/");
				int i = h1.lastIndexOf(".");
				String ext = h1.substring(i,h1.length()-1);
				String fileName =UUID.randomUUID().toString()+ext;
				
				ActorDao.getDao().updateActor(name, introduce ,fileName,Integer.parseInt(id));
				part.write(filepath+"/"+fileName);
				
				part.delete();
			
				String h = request.getHeader("referer");
				response.sendRedirect(h);
			}
		}
	//修改导演
		
		protected void modifyDirector(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String name =request.getParameter("name");
			String id =request.getParameter("id");
			Part part =request.getPart("image");//判断有没有修改图片
			String introduce =request.getParameter("introduce");
			String h1 = part.getHeader("content-disposition");
			
			
			if(h1.contains("filename=\"\"")) {
				DirectorDao.getDao().updateDirector(name, introduce,Integer.parseInt(id));
				String h = request.getHeader("referer");
				response.sendRedirect(h);
			}else {
			
				String filepath = request.getServletContext().getRealPath("/images/");
				int i = h1.lastIndexOf(".");
				String ext = h1.substring(i,h1.length()-1);
				String fileName =UUID.randomUUID().toString()+ext;
				
				DirectorDao.getDao().updateDirector(name,introduce,fileName,Integer.parseInt(id));
				part.write(filepath+"/"+fileName);
				
				part.delete();
			
				String h = request.getHeader("referer");
				response.sendRedirect(h);
			}
		}
	//删除地区
	protected void getCountryList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
		MovieCountryDao.getDao().deleteById(Integer.parseInt(id));
		
		ArrayList<MovieCountry> countryList = MovieCountryDao.getDao().getCountryList();
		request.setAttribute("countryList", countryList);
		request.getRequestDispatcher("/countrylist.jsp").forward(request, response);
	}

	//删除类型
	
	protected void getTypeList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
		MovieTypeDao.getDao().deleteById(Integer.parseInt(id));
		ArrayList<MovieType> typeList = MovieTypeDao.getDao().getTypeList();
		request.setAttribute("typeList", typeList);
		request.getRequestDispatcher("/typelist.jsp").forward(request, response);
	}

	//删除语言
		
	protected void getLanguageList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
		MovieLanguageDao.getDao().deleteById(Integer.parseInt(id));
		ArrayList<MovieLanguage> typeList = MovieLanguageDao.getDao().getLanguageList();
		request.setAttribute("typeList", typeList);
		request.getRequestDispatcher("/languagelist.jsp").forward(request, response);
	}

	//导演分页
	
	protected void getDirectorList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String page =request.getParameter("page");
			String id = request.getParameter("id");
			if(id!=null) {
				DirectorDao.getDao().deleteById(Integer.parseInt(id));
				MovieDirectorDao.getMovieDirectorDao().deleteDirectorMovie(Integer.parseInt(id));
			}
			ArrayList<Director> directorList = DirectorDao.getDao().getDirectorList(Integer.parseInt(page)-1);
			int actorCount = ActorDao.getDao().getAllCount();
			int directorCount = DirectorDao.getDao().getAllCount();
			
			request.setAttribute("directorList", directorList);
			request.setAttribute("actorCount",actorCount);
			request.setAttribute("directorCount",directorCount);
			request.getRequestDispatcher("/directorlist.jsp").forward(request, response);
			}
	
	 
	//演员分页
	
		protected void getActorList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String page =request.getParameter("page");
		String id = request.getParameter("id");
		if(id!=null) {
			ActorDao.getDao().deleteById(Integer.parseInt(id));
			MovieActorDao.getMovieActorDao().deleteActorMovie(Integer.parseInt(id));
		}
		ArrayList<Actor> actorList = ActorDao.getDao().getActorList(Integer.parseInt(page)-1);
		int actorCount = ActorDao.getDao().getAllCount();
		int directorCount = DirectorDao.getDao().getAllCount();
		
		request.setAttribute("actorList", actorList);
		request.setAttribute("actorCount",actorCount);
		request.setAttribute("directorCount",directorCount);
		request.getRequestDispatcher("/actorlist.jsp").forward(request, response);
		}
	//转到信息维护界面
		protected void requestMessagejsp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			ArrayList<Actor> actorList = ActorDao.getDao().getActorList();
			ArrayList<Director> directorList = DirectorDao.getDao().getDirectorList();
			ArrayList<MovieType> typeList = MovieTypeDao.getDao().getTypeList();
			ArrayList<MovieLanguage> languageList = MovieLanguageDao.getDao().getLanguageList();
			ArrayList<MovieCountry> countryList = MovieCountryDao.getDao().getCountryList();
			int actorCount = ActorDao.getDao().getAllCount();
			int directorCount = DirectorDao.getDao().getAllCount();
			
			request.setAttribute("actorCount",actorCount);
			request.setAttribute("directorCount",directorCount);
			request.setAttribute("actorList",actorList );
			request.setAttribute("directorList",directorList );
			request.setAttribute("typeList",typeList );
			request.setAttribute("languageList",languageList );
			request.setAttribute("countryList",countryList );
			
			request.getRequestDispatcher("/message.jsp").forward(request, response);
		}
	
	
	//获得要搜索的电影的举报记录
		protected void getMovieReportList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String movie =request.getParameter("movie");
		HttpSession session =request.getSession();
		Maneger m =(Maneger)session.getAttribute("maneger");
		
		ArrayList<Report> reportList = ReportDao.getObj().getReportList(movie,m.getId());
		
		request.setAttribute("reportList", reportList);
		request.getRequestDispatcher("/reportDeal.jsp").forward(request, response);
			
		}
	//获得举报记录
		protected void getReportHistory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //举报信息
			 HttpSession session =request.getSession();
			 Maneger m =(Maneger)session.getAttribute("maneger");
			 int pagemax=ReportDao.getObj().getReportRecordsCount();//举报数量
			 int addpagemax=MovieDao.getMovieDao().addmovierecordsmax(m.getId());//添加记录数量
			 int deletepagemax=MovieDao.getMovieDao().deletemovierecordsmax(m.getId());//删除数量
			 int modfinpagemax=MovieDao.getMovieDao().addmovierecordsmax(m.getId());//修改的记录数量
			 pagemax=(pagemax+5)/6;//举报页数
			 addpagemax=(addpagemax+5)/6;//添加页数
			 deletepagemax=(deletepagemax+5)/6;//删除页数
			 modfinpagemax=(modfinpagemax+5)/6;//修改页数
					ArrayList<Report> reportLIst = ReportDao.getObj().getReportHistory(m.getId(),1);//举报集合
					ArrayList<AddMovieRecords> addLIst=MovieDao.getMovieDao().addrecordscontent(m.getId(), 1);//添加集合
					ArrayList<DeleteMovieRecords> deleteLIst=MovieDao.getMovieDao().deleterecordscontent(m.getId(), 1);//删除结合
					ArrayList<MonfinMovieRecords> modfinLIst=MovieDao.getMovieDao().ModfinRecordscontent(m.getId(), 1);//修改集合
					request.setAttribute("thispage", 1);
					request.setAttribute("addthispage", 1);
					request.setAttribute("deletethispage", 1);
					request.setAttribute("modfinthispage", 1);
					//System.err.println("11111111111111");
					request.setAttribute("addpagemax", addpagemax);
					request.setAttribute("deletepagemax", deletepagemax);
					request.setAttribute("modfinpagemax", modfinpagemax);
					request.setAttribute("pagemax", pagemax);
					//System.out.println("222222222222");
					request.setAttribute("reportLIst", reportLIst);
					request.setAttribute("addList", addLIst);
					request.setAttribute("deleteList", deleteLIst);
					request.setAttribute("modfinList", modfinLIst);
//					/System.out.println("```````````````");
					request.getRequestDispatcher("/reportlist.jsp").forward(request, response);
		}
		//举报记录分页ajax
		
		protected void getReportHistoryAjax(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			 String thispage=request.getParameter("thispage");
             HttpSession session =request.getSession();
			 Maneger m =(Maneger)session.getAttribute("maneger");
			 int pagemax=ReportDao.getObj().getReportRecordsCount();
			 pagemax=(pagemax+5)/6;
				 ArrayList<Report> reportLIst = ReportDao.getObj().getReportHistory(m.getId(),Integer.parseInt(thispage));
				 request.setAttribute("thispage", thispage);
				 request.setAttribute("pagemax", pagemax);
				 request.setAttribute("reportLIst", reportLIst);
				// System.out.println(reportLIst);
				 request.getRequestDispatcher("/reportlistrecords.jsp").forward(request, response);
			 }
		//添加记录分页ajax
		protected void getAddHistoryAjax(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String addthispage=request.getParameter("addthispage");
            HttpSession session =request.getSession();
			 Maneger m =(Maneger)session.getAttribute("maneger");
			 
			 int addpagemax=MovieDao.getMovieDao().addmovierecordsmax(m.getId());
			 addpagemax=(addpagemax+5)/6;
			ArrayList<AddMovieRecords>addList=MovieDao.getMovieDao().addrecordscontent(m.getId(),Integer.parseInt(addthispage));	
			     request.setAttribute("addthispage", addthispage);
				 request.setAttribute("addpagemax", addpagemax);
				 request.setAttribute("addList", addList);
				// System.out.println(reportLIst);
				 request.getRequestDispatcher("/addlistrecords.jsp").forward(request, response);
		}
		
		//删除记录分页ajax
		protected void getDeleteHistoryAjax(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String deletethispage=request.getParameter("deletethispage");
            HttpSession session =request.getSession();
			 Maneger m =(Maneger)session.getAttribute("maneger");
			 
			 int deletepagemax=MovieDao.getMovieDao().deletemovierecordsmax(m.getId());
			 deletepagemax=(deletepagemax+5)/6;
			 ArrayList<DeleteMovieRecords>deleteList=MovieDao.getMovieDao().deleterecordscontent(m.getId(), Integer.parseInt(deletethispage));
			     request.setAttribute("deletethispage", deletethispage);
				 request.setAttribute("deletepagemax", deletepagemax);
				 request.setAttribute("deleteList", deleteList);
				// System.out.println(reportLIst);
				 request.getRequestDispatcher("/deletelistrecords.jsp").forward(request, response);
		}
		
		//修改记录分页Ajax
		protected void getModfinHistoryAjax(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String modfinthispage=request.getParameter("modfinthispage");
            HttpSession session =request.getSession();
			 Maneger m =(Maneger)session.getAttribute("maneger");
			 
			 int modfinpagemax=MovieDao.getMovieDao().ModfinMovieRecordsmax(m.getId());
			 modfinpagemax=(modfinpagemax+5)/6;
			 ArrayList<MonfinMovieRecords>modfinList=MovieDao.getMovieDao().ModfinRecordscontent(m.getId(), Integer.parseInt(modfinthispage));
			     request.setAttribute("modfinthispage", modfinthispage);
				 request.setAttribute("modfinpagemax", modfinpagemax);
				 request.setAttribute("modfinList",modfinList);
				// System.out.println(reportLIst);
				 request.getRequestDispatcher("/modfinlistrecords.jsp").forward(request, response);
		}
	//获得举报数量
		protected void getReportCount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			int count = ReportDao.getObj().getReportCount();
			PrintWriter pw = response.getWriter();
			pw.print(count+"");
		}
	
	//转发到管理员首页
	protected void requestManeger(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int count = ReportDao.getObj().getReportCount();
		
		request.setAttribute("count", count);
		request.getRequestDispatcher("/maneger.jsp").forward(request, response);
	}
	
	//修改电影
		protected void modifyMovie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String movieName = request.getParameter("movieName");
			String movieId = request.getParameter("movieId");
			String movieIntroduce =request.getParameter("movieIntroduce");
			String movieShow =request.getParameter("movieShow");
			HttpSession session =request.getSession();
			Maneger m =(Maneger)session.getAttribute("maneger");
			Movie movies=new Movie();
			boolean shows=false;
			int show=0;
			if(Integer.parseInt(movieShow)==1) {
				show=1;
				shows=true;
			}
			
			Part part =request.getPart("movieImage");//判断有没有修改图片
			String h1 = part.getHeader("content-disposition");
			
			
			if(h1.contains("filename=\"\"")) {
				MovieDao.getMovieDao().motifyMovie(Integer.parseInt(movieId), movieName, movieIntroduce, show);
				String h = request.getHeader("referer");
				movies.setMovieIntroduction(movieIntroduce);
				movies.setMovieShow(shows);			
				movies.setMovieName(movieName);
				MovieDao.getMovieDao().modfinmovierecords(movies,m.getId());
				response.sendRedirect(h);
			}else {
			
				String filepath = request.getServletContext().getRealPath("/images/");
				int i = h1.lastIndexOf(".");
				String ext = h1.substring(i,h1.length()-1);
				String fileName =UUID.randomUUID().toString()+ext;
				part.write(filepath+"/"+fileName);
				
				part.delete();
				
				movies.setMovieName(movieName);
				movies.setMovieImage(fileName);
				movies.setMovieIntroduction(movieIntroduce);
				movies.setMovieShow(shows);
				
				MovieDao.getMovieDao().modfinmovieimagerecords(movies,m.getId());
				MovieDao.getMovieDao().motifyMovie(Integer.parseInt(movieId), movieName, movieIntroduce, show,fileName);
				String h = request.getHeader("referer");
				response.sendRedirect(h);
			}
			
		}

	//找到要修改的电影
		protected void getModifyMovie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String movie = request.getParameter("mid");
			
			Movie movie2 = MovieDao.getMovieDao().getMovieById(Integer.parseInt(movie));
			JSONObject js =JSONObject.fromObject(movie2);
			String movies=js.toString();
			
			PrintWriter pw =response.getWriter();
			pw.print(movies);
		}
	
	
	//找到要删除的电影
	protected void findDeleteMovie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String movie = request.getParameter("movie");
		ArrayList<Movie> movieList = MovieDao.getMovieDao().getMovieList(movie);
		
		JSONArray js =JSONArray.fromObject(movieList);
		String list=js.toString();
		
		PrintWriter pw =response.getWriter();
		pw.print(list);
	}

	
	//转发到修改电影页面
	protected void requestModifymovie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//电影列表
		ArrayList<Movie> movieList = MovieDao.getMovieDao().getMovieList();
		int pagemax=(MovieDao.getMovieDao().pagemax()+7)/8;
		request.setAttribute("thispage", 1);
		request.setAttribute("pagemax", pagemax);
		request.setAttribute("movieList", movieList);
		request.getRequestDispatcher("/modifymovie.jsp").forward(request, response);
	}

	
	//不同意审核举报
			protected void disagreeReport(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				String rid = request.getParameter("rid");
				
				HttpSession session =request.getSession();
				Maneger maneger=(Maneger)session.getAttribute("maneger");
			
				ReportDao.getObj().agreeReport(Integer.parseInt(rid), maneger.getId());
			}
	
	//同意审核举报
		protected void agreeReport(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String rid = request.getParameter("rid");
			
			HttpSession session =request.getSession();
			Maneger maneger=(Maneger)session.getAttribute("maneger");
			
			
			ReportDao.getObj().agreeReport(Integer.parseInt(rid), maneger.getId());
			int dicussId = DiscussDao.getObj().getDicussId(Integer.parseInt(rid));
			
			DiscussDao.getObj().reWriteDicussId(dicussId);
			
			
		}

	//转发到管理举报页面
		protected void requestManegerReport(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			ArrayList<Report> reportList = ReportDao.getObj().getReport();
			request.setAttribute("reportList", reportList);
			
			request.getRequestDispatcher("/manegeReport.jsp").forward(request, response);
		}
	
	//删除电影
		protected void deleteMovie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String movie =request.getParameter("movie");
		String thispage=request.getParameter("thispage");
		
		HttpSession session =request.getSession();
	    Maneger  maneger = (Maneger)session.getAttribute("maneger");
	    
	    Movie movieid=MovieDao.getMovieDao().getMovieById(Integer.parseInt(movie));//找到要删除的电影id
		MovieDao.getMovieDao().deleteMovie(Integer.parseInt(movie));//删除电影
        MovieDao.getMovieDao().deletemovierecords(movieid.getMovieName(), maneger.getId());//做记录
        
		CollectDao.getObj().CollectDelete(movie);
		ClickDao.getObj().quxiaodianzan(Integer.parseInt(movie));
		DiscussDao.getObj().deleteDiscuss(Integer.parseInt(movie));
		GiveGradesDao.getObj().deleteGrades(Integer.parseInt(movie));
		MovieDirectorDao.getMovieDirectorDao().deleteDirector(Integer.parseInt(movie));
		MovieActorDao.getMovieActorDao().deleteActor(Integer.parseInt(movie));
		ArrayList<Integer> list = DiscussDao.getObj().getDiscussId(Integer.parseInt(movie));
		String ids = "";
		for(int i=0;i<list.size();i++) {
			ids=ids+list.get(i)+",";
		}
		if(ids.equals("")) {
			
		}else {
			ids = ids.substring(0,ids.length()-1);
			ReportDao.getObj().deleteReport(ids);
		}
		int pagemax=(MovieDao.getMovieDao().pagemax()+7)/8;
		ArrayList<Movie> movieList = MovieDao.getMovieDao().thispagemovie(Integer.parseInt(thispage));

		request.setAttribute("movieList", movieList);
		request.setAttribute("pagemax", pagemax);
		request.getRequestDispatcher("/movielist.jsp").forward(request, response);
		}
		
	 //获得要删除电影
	protected void findMovie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String movie = request.getParameter("movie");
		
		ArrayList<Movie> deleteMovieList = MovieDao.getMovieDao().getMovieList(movie);
	
		JSONArray js =JSONArray.fromObject(deleteMovieList);
		String lists =js.toString();
		//System.out.println(lists);
		PrintWriter pw =response.getWriter();
		if(lists.equals("[]")) {
			
			pw.print("0");
			
		}else {
			pw.print(lists);
		}
	}
	
	 //转发到删除电影页面
	protected void requestDeleteMovie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ArrayList<Movie> movieList = MovieDao.getMovieDao().getMovieList();
		if(movieList==null) {
			return;
		}else {
			int pagemax=(MovieDao.getMovieDao().pagemax()+7)/8;
			request.setAttribute("thispage", 1);
			request.setAttribute("pagemax", pagemax);
			request.setAttribute("movieList", movieList);
			request.getRequestDispatcher("/deletemovie.jsp").forward(request, response);
		}
	}
	//分页显示删除电影
	protected void pagedeletemovie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String thispage=request.getParameter("thispage");
		int pagemax=(MovieDao.getMovieDao().pagemax()+7)/8;
		ArrayList<Movie>movieList=MovieDao.getMovieDao().thispagemovie(Integer.parseInt(thispage));
		request.setAttribute("movieList", movieList);
		request.setAttribute("pagemax", pagemax);
		
		//System.out.println(pagemax);
		request.getRequestDispatcher("/movielist.jsp").forward(request, response);
		
	}
	 //获得用户，设置管理员包含页面
	protected void getManeger(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session =request.getSession();
		Maneger maneger = (Maneger)session.getAttribute("maneger");
		request.setAttribute("maneger", maneger);
	}
	 //判断登录
	    protected void checkLog(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name =request.getParameter("name");
		String password =request.getParameter("password");
		String code =request.getParameter("code");
		
		HttpSession session =request.getSession();
		String codes =(String)session.getAttribute("code");
		
		if(code.equalsIgnoreCase(codes)) {
			Maneger manegers =new Maneger();
			manegers.setManegerName(name);
			manegers.setManegerPassword(password);
			
			Maneger maneger = ManegerDao.getDao().logManeger(manegers);
			
			if(maneger==null) {
				response.sendRedirect(request.getContextPath()+"/manegerlog.jsp?error=2");
			}else {
			//管理员个人页面
			session.setAttribute("maneger", maneger);
			
				
			response.sendRedirect(request.getContextPath()+"/ManegerServlet?action=requestManeger");	
				
			}
			
		}else {
			response.sendRedirect(request.getContextPath()+"/manegerlog.jsp?error=1");
		}
	
	}
     //  生成登录验证码
		protected void creatCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			BufferedImage bi =new BufferedImage(80, 30, BufferedImage.TYPE_INT_RGB);
			
			Graphics g = bi.getGraphics();
			g.setFont(new Font("",Font.BOLD,25));
			
			String code ="";
			Random r =new Random();
			for(int i=0;i<4;i++) {
				int math =r.nextInt(123);
				
				if((math>='a'&&math<='z')||(math>='A'&&math<='Z')||(math>='0'&&math<='9')) {
					code+=(char)math;
				}else {
					i--;
				}
			}
			g.setColor(new Color(255,255,255));
			g.fillRect(0, 0, 100, 30);
			g.setColor(new Color(0,0,0));
			g.drawString(code, 10, 25);
			OutputStream os =response.getOutputStream();
			ImageIO.write(bi, "jpg", os);
			HttpSession session =request.getSession();
			session.setAttribute("code",code );
			
		}
  
		
    //  添加电影
		protected void submitMovie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			Part part1 =request.getPart("movieImage");
			Part part2 =request.getPart("moviePoster");
			part1.getInputStream();                     
			String h1 = part1.getHeader("content-disposition");
			String h2 = part2.getHeader("content-disposition");
			String filepath = request.getServletContext().getRealPath("/images/");
			int i1 = h1.lastIndexOf(".");
			String ext1 = h1.substring(i1,h1.length()-1);
			int i2 = h2.lastIndexOf(".");
			String ext2 = h2.substring(i2,h2.length()-1);
			
			String filename1=UUID.randomUUID().toString()+ext1;
			String filename2=UUID.randomUUID().toString()+ext2;
			part1.write(filepath+"/"+filename1);
			part2.write(filepath+"/"+filename2);
			ImageSize.getimage().setImage(filepath+"/"+filename1);
			ImageSize.getimage().setPoster(filepath+"/"+filename2);
			
			part1.delete();
			part2.delete();
			
			HttpSession session =request.getSession();
		    Maneger  maneger = (Maneger)session.getAttribute("maneger");
			
			
			String name = request.getParameter("movieName");
			String time = request.getParameter("movieTime");
			String language = request.getParameter("movieLanguage");
			String type = request.getParameter("movieType");
			String country = request.getParameter("movieCountry");
			String introduce = request.getParameter("movieIntroduce");
			String show = request.getParameter("movieShow");
			String actor = request.getParameter("movieActor");
			String director = request.getParameter("movieDirector");
			String showTime =request.getParameter("showTime");
			showTime+=" 00:00:00.00000000";
		
			Timestamp showtimes =Timestamp.valueOf(showTime);
			
			Movie movie = new Movie();
			movie.setManegerid(maneger);
			movie.setMovieName(name);
			movie.setMovieTime(Integer.parseInt(time));
			movie.setMovieShowtime(showtimes);
			
			MovieLanguage l = new MovieLanguage();
			l.setId(Integer.parseInt(language));
			movie.setMovieLanguageId(l);
			
			MovieType m =new MovieType();
			m.setId(Integer.parseInt(type));
			movie.setMovieTypeId(m);
			
			MovieCountry c =new MovieCountry();
			c.setId(Integer.parseInt(country));
			movie.setMovieCountryId(c);
			
			movie.setMovieIntroduction(introduce);
			//bit值 待确认
			if(show.equals("0")) {
				movie.setMovieShow(false);
			}else {
				movie.setMovieShow(true);
			}
			
			movie.setMovieImage(filename1);
			movie.setMoviePoster(filename2);
			
			MovieDao.getMovieDao().addMovie(movie);//添加电影movie表
			Movie newMovie = MovieDao.getMovieDao().getNewMovie(maneger.getId());//最新添加的电影
			
			actor= actor.substring(0,actor.length()-1);
			director= director.substring(0,director.length()-1);
			
			MovieActorDao.getMovieActorDao().addMovieActor(actor, newMovie.getId());
			MovieDirectorDao.getMovieDirectorDao().addMovieDirector(director, newMovie.getId());
			MovieDao.getMovieDao().addmovierecords(name,maneger.getId());//添加电影记录
			String head =request.getHeader("referer");
			response.sendRedirect(head);
			
		}
	
	
	//  语言列表
		protected void languageList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			ArrayList<MovieLanguage> languageList = MovieLanguageDao.getDao().getLanguageList();
			JSONArray list = JSONArray.fromObject(languageList);
			
			String lists =list.toString();
			
			PrintWriter pw =response.getWriter();
			pw.print(lists);
			
		}
    //类型列表
		protected void typeList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			ArrayList<MovieType> typeList = MovieTypeDao.getDao().getTypeList();
			JSONArray list = JSONArray.fromObject(typeList);
			
			String lists =list.toString();
			
			PrintWriter pw =response.getWriter();
			pw.print(lists);
		}
	//地区列表
		protected void countryList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			ArrayList<MovieCountry> countryList = MovieCountryDao.getDao().getCountryList();
            JSONArray list = JSONArray.fromObject(countryList);
			
			String lists =list.toString();
			
			PrintWriter pw =response.getWriter();
			pw.print(lists);
		}
	//添加语言
		protected void addLanguage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String language =request.getParameter("language");
			MovieLanguageDao.getDao().add(language);
			
			String h = request.getHeader("referer");
			response.sendRedirect(h+"?error=1");
		}
	//添加类型
		protected void addType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String type =request.getParameter("type");	
			MovieTypeDao.getDao().add(type);
			
			String h = request.getHeader("referer");
			response.sendRedirect(h+"?error=1");
		}
	//添加地区
		protected void addCountry(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			String country =request.getParameter("country");
			MovieCountryDao.getDao().add(country);
			
			String h = request.getHeader("referer");
			response.sendRedirect(h+"?error=1");
		}
	//查找演员
			protected void findActor(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				// TODO Auto-generated method stub
				
				String name = request.getParameter("name");
				ArrayList<Actor> actorList =ActorDao.getDao().findByName(name);
				
				JSONArray js =JSONArray.fromObject(actorList);
				String lists =js.toString();
				
				PrintWriter pw =response.getWriter();
				if(lists.equals("[]")) {
					
					pw.print("0");
					
				}else {
					pw.print(lists);
				}
			}

	
	//添加演员
	protected void addActor(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		
		Part part =request.getPart("image");
		
		
		String h = part.getHeader("content-disposition");
		String filepath = request.getServletContext().getRealPath("/images/");
		int i = h.lastIndexOf(".");
		String ext = h.substring(i,h.length()-1);
		String filename=UUID.randomUUID().toString()+ext;
		part.write(filepath+"/"+filename);
		ImageSize.getimage().setImage(filepath+"/"+filename);
		part.delete();
	
		String name =request.getParameter("name");
		String introduce =request.getParameter("introduce");		
		
		ActorDao.getDao().addByName(name, filename,introduce);
		String hh = request.getHeader("referer");
		response.sendRedirect(hh+"?error=1");
	}
	
	//添加导演
	protected void addDirector(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		//response.getWriter().append("Served at: ").append(request.getContextPath());
        Part part =request.getPart("image");
		
		
		String h = part.getHeader("content-disposition");
		String filepath = request.getServletContext().getRealPath("/images/");
		int i = h.lastIndexOf(".");
		String ext = h.substring(i,h.length()-1);
		String filename=UUID.randomUUID().toString()+ext;
		part.write(filepath+"/"+filename);
		ImageSize.getimage().setImage(filepath+"/"+filename);
		part.delete();
	
		String name =request.getParameter("name");
		String introduce =request.getParameter("introduce");
		DirectorDao.getDao().addByName(name, filename,introduce);
		String hh = request.getHeader("referer");
		response.sendRedirect(hh+"?error=1");
	}
	//转发到添加电影的页面
	protected void requestAddMovie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		//导演列表
		ArrayList<Director> directorList = DirectorDao.getDao().getDirectorList();
		//演员列表
		ArrayList<Actor> actorList = ActorDao.getDao().getActorList();
		//类型列表
		ArrayList<MovieType> typeList = MovieTypeDao.getDao().getTypeList();
		//地区列表
		ArrayList<MovieCountry> countryList = MovieCountryDao.getDao().getCountryList();
				
		//语言列表
		ArrayList<MovieLanguage> languageList = MovieLanguageDao.getDao().getLanguageList();
		
		
		request.setAttribute("typeList", typeList);
		request.setAttribute("countryList", countryList);
		request.setAttribute("directorList", directorList);
		request.setAttribute("languageList", languageList);
		request.setAttribute("actorList", actorList);
		request.getRequestDispatcher("/addmovie.jsp").forward(request, response);
	}

	//查找导演
		protected void findDirector(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			// TODO Auto-generated method stub
			
			String name = request.getParameter("name");
			ArrayList<Director> directorList = DirectorDao.getDao().findByName(name);
			
			JSONArray js =JSONArray.fromObject(directorList);
			String lists =js.toString();
			
			PrintWriter pw =response.getWriter();
			if(lists.equals("[]")) {
				
				pw.print("0");
				
			}else {
				pw.print(lists);
			}
		}

	/** 
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
